package com.redfox.newapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.redfox.newapp.MyApplication;
import com.redfox.newapp.R;
import com.redfox.newapp.activity.WebActivity;

import java.util.ArrayList;

/**
 * Created by redfox on 2017/3/16.
 */

public class AndDevAdapter extends RecyclerView.Adapter<AndDevAdapter.AndDevViewHolder> {
    private final String TAG = "AndDevAdapter";

    private ArrayList<ItemData> mItems;
    private Context mContext;

    public static class ItemData {
        public String imageUrl;
        public String title;
        public String who;
        public String publish_date;
        public String url;
    }

    public static class AndDevViewHolder extends RecyclerView.ViewHolder {
        public ImageView image;
        public TextView title;
        public TextView who;
        public TextView publish_date;
        public AndDevViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.photo_thumb);
            title = (TextView) itemView.findViewById(R.id.title);
            who = (TextView) itemView.findViewById(R.id.who);
            publish_date = (TextView) itemView.findViewById(R.id.publish_date);
        }
    }


    public AndDevAdapter(Context context) {
        super();
        mItems = new ArrayList<>();
        mContext = context;
    }

    @Override
    public AndDevViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.anddev_item_layout, parent, false);
        return new AndDevViewHolder(v);
    }

    @Override
    public void onBindViewHolder(AndDevViewHolder holder, int position) {
        ItemData item = (ItemData) mItems.get(position);

        //holder.image.setImageDrawable(ContextCompat.getDrawable(MyApplication.getInstance().getApplicationContext(), R.drawable.unknown_people));
        Glide.with(mContext)
                .load(item.imageUrl)
                .placeholder(R.drawable.unknown_people)
                .centerCrop()
                .fitCenter()
                .dontAnimate()
                .into(holder.image);

        holder.title.setText(item.title);
        holder.who.setText(item.who);
        holder.publish_date.setText(item.publish_date);

        final String url = item.url;

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, WebActivity.class);
                intent.putExtra("url", url);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    public void addItem(ItemData item) {
        mItems.add(item);
    }


}
